package com.sc.server.system.test.springTimer;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
public class TimeRange {
    String startTime;
    String endTime;
    long minute;
    long hour;
    List<String> times=new ArrayList<>();

    public TimeRange(String startTime, String endTime,Long frequency) {
        this.startTime = startTime;
        this.endTime = endTime;
        if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
             setHourAndMinuteByFreq(frequency);
             times.add(startTime);
             String time=startTime;

              while (true){
                  String[] split = time.split(":");
                  Long lastHour=Long.parseLong(split[0]);
                  Long lastMinute=Long.parseLong(split[1]);
                  long m = lastMinute + minute;
                  if(m>59){
                      m=m-60;
                      lastHour++;
                  }
                  time=((lastHour+hour)<10?"0":"") +(lastHour+hour)+":"+(m<10?"0":"")+ m;
                    if(time.compareTo(endTime)>=1){
                        break;
                    }
                    times.add(time);
                }
        }

    }

    /**
     * 根据采集频率计算间隔小时分钟
     * @param frequency
     */
    private void setHourAndMinuteByFreq(Long frequency) {
        frequency= frequency / (1000*60);
        if(frequency>60){
            hour=Math.floorDiv(frequency,60);

        }
        minute= Math.floorMod(frequency,60);
    }


}
